package show;

import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.util.*;

public class GoogleChartApi extends Thread implements Runnable {
  public static final String PIECHART = "p";
  public static final String BARCHART = "bvs";
  public static final String LINECHART = "lc";
  
  private ArrayList<ChartInfo> slices;
  private JPanel parent;
  private String about;
  private String chartType;
  private JTabbedPane tabs;
  
  public GoogleChartApi(ArrayList<ChartInfo> slices, JPanel parent, String about, String type, JTabbedPane tabs) {
    this.slices = slices;
    this.parent = parent;
    this.about = about;
    this.chartType = type;
    this.tabs = tabs;
  }
    
  @Override
  public void run() {
    try {
      
      // Porcentagens ---------------------------------------------------------------------------
      String u = "http://chart.apis.google.com/chart?chs=545x330&cht=" + chartType + "&chds=a&chd=t:";
      for(int i = 0; i < slices.size(); i++) {
        u += this.slices.get(i).getValue();
      
        if (i != slices.size() - 1)
          u += ",";
      }
      
      // Legenda --------------------------------------------------------------------------------
      if (chartType.equals(PIECHART)) {
        u += "&chdl=";
        for(int i = 0; i < slices.size(); i++) {
          u += (this.slices.get(i).getLabel() + " (" + this.slices.get(i).getValue() + ")").replace(' ', '+');
      
          if (i != slices.size() - 1)
            u += "|";
        }
      }
      u += "&chtt=" + about.replace(' ', '+');
      
      // Eixos ----------------------------------------------------------------------------------
      if (chartType.equals(BARCHART)) {
    	u += "&chxt=x,y&chxl=0:|";
    	
    	for(int i = 0; i < slices.size(); i++) {
    	  u += this.slices.get(i).getLabel().replace(' ', '+');
    	  
    	  if (i != slices.size() - 1)
    		u += "|";
    	}
    	
    	
    	u += "&chbh=a";
      }
      if (chartType.equals(LINECHART)) {
    	  u += "&chxt=x,y&chxl=0:|";
      	
      	for(int i = 0; i < slices.size(); i++) {
      	  u += this.slices.get(i).getLabel().replace(' ', '+');
      	  
      	  if (i != slices.size() - 1)
  		    u += "|";
      	}
      }
      
      URL url = new URL(u);

      try {
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("HEAD");
        if(con.getResponseCode() != HttpURLConnection.HTTP_OK) {
          return;
        }
      }
      catch (Exception e) { return; }

      Image image = Toolkit.getDefaultToolkit().createImage(url);
      JLabel imageHolder = new JLabel();
      imageHolder.setIcon(new ImageIcon(image));
      
      int index = this.tabs.indexOfComponent(parent);
      this.tabs.setComponentAt(index, imageHolder);
      this.tabs.revalidate();
    }
    catch (Exception ex) { return; }
  }
  
}
